﻿'ADAM CORTEZ
Imports DBUtilitiesLibrary

Public Class RegisterStudent
    Inherits System.Web.UI.Page
    Dim pass As Boolean = True  ' checks validation of all controls
    Dim currentOK As Boolean = True  ' checks validation of current control
    Dim ws As New DO_NOT_MOVE_ME_FROM_THIS_PROJECT.DARSWebservice
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ''Check if user is logged in
        If Session("CurrentUser") Is Nothing Then
            Response.Redirect("NotLoggedIn.aspx")
            Exit Sub
        End If

        If Not IsPostBack Then
            pnlUserInfo.Visible = False
            lblValidateExist.Visible = False
        End If

    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        lblValidate.Visible = False
        validatorBottom()
        If pass Then
            createStudent(txtUsername.Text, txtLastName.Text, txtFirstName.Text, txtAddress.Text, txtCity.Text, ddlState.Text, txtZipcode.Text, txtEmail.Text, txtUsername.Text, CStr(1))
            addSecurityQuestions(txtUsername.Text, Q1txt.Text, A1txt.Text, Q2txt.Text, A2txt.Text)
            lblComplete.Text = " Thank You! Your application has been processed"
        End If
    End Sub

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
        clearValues()
    End Sub

    Protected Sub clearValues()
        txtFirstName.Text = ""
        txtLastName.Text = ""
        txtAddress.Text = ""
        txtCity.Text = ""
        txtZipcode.Text = ""
        txtPhone.Text = ""
        txtUsername.Text = ""
        txtPassword.Text = ""
        txtConfirmPassword.Text = ""
        txtEmail.Text = ""
        txtConfirmEmail.Text = ""
        txtSocial.Text = ""
        Q1txt.Text = ""
        A1txt.Text = ""
        Q2txt.Text = ""
        A2txt.Text = ""
    End Sub

    Protected Sub btnCheck_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCheck.Click
        validatorTop()
        If pass Then

            If checkUserExists(txtUsername.Text) Then
                lblValidateExist.Visible = True
            Else
                lblValidateExist.Visible = False
                createUser(txtUsername.Text, txtPassword.Text, 1)
                pnlUserInfo.Visible = True
            End If


            'ws.createUser(txtUsername.Text, txtPassword.Text, 1)

        End If

    End Sub

    Sub validatorTop()

        lblValidate.Text = ""

        Dim regUser As String = "^[A-Za-z]\w{6,}$"
        Dim regUserMessage As String = "Username must start with a letter and is at least 7 characters long"


        Dim regPass As String = "^[A-Za-z]\w{7,}$"
        Dim regPassMessage As String = "Password must start with a letter and is at least 8 characters long"

        While currentOK
            currentOK = regExCheckTop(txtUsername.Text, regUser, regUserMessage)
            currentOK = regExCheckTop(txtPassword.Text, regPass, regPassMessage)
            Return
        End While
    End Sub

    Sub validatorBottom()

        lblComplete.Text = ""

        Dim regFName As String = "[A-Za-z]"
        Dim regFNameMessage As String = "First Name must be characters"

        Dim regLName As String = "[A-Za-z]"
        Dim regLNameMessage As String = "Last Name must be characters"

        Dim regAddress As String = "^\d{1,9}\s[A-Za-z]+\s[A-Za-z]+\.?$"
        Dim regAddressMessage As String = "Address must be 1-9 digits followed by a space followed by characters (street name) followed by a space followed by characters(street type)"

        Dim regCity As String = "^[A-Za-z]{3,}$"
        Dim regCityMessage As String = "City name must be characters"

        Dim regZip As String = "^\d{5}(-\d{4})?$"
        Dim regZipMessage As String = "Zip code must be 5 digits (optional followed by - and a 4 digit extension)"

        Dim regPhone As String = "^([\(]{1}[0-9]{3}[\)]{1}[\.| |\-]{0,1}|^[0-9]{3}[\.|\-| ]?)?[0-9]{3}(\.|\-| )?[0-9]{4}$"
        Dim regPhoneMessage As String = "phone number is not valid"

        Dim regEmail As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
        Dim regEmailMessage As String = "Email is not valid"

        Dim regSSN As String = "^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}$"
        Dim regSSNMessage As String = "SSN is not valid"






        While currentOK
            currentOK = regExCheckBottom(txtFirstName.Text, regFName, regFNameMessage)
            currentOK = regExCheckBottom(txtLastName.Text, regLName, regLNameMessage)
            currentOK = regExCheckBottom(txtAddress.Text, regAddress, regAddressMessage)
            currentOK = regExCheckBottom(txtCity.Text, regCity, regCityMessage)
            currentOK = regExCheckBottom(txtZipcode.Text, regZip, regZipMessage)
            currentOK = regExCheckBottom(txtPhone.Text, regPhone, regPhoneMessage)
            currentOK = regExCheckBottom(txtEmail.Text, regEmail, regEmailMessage)
            currentOK = regExCheckBottom(txtSocial.Text, regSSN, regSSNMessage)

            Return
        End While

    End Sub

    Function regExCheckTop(ByRef control As String, ByVal expression As String, ByVal message As String) As Boolean

        If Not Regex.IsMatch(control, expression) Then
            lblValidate.Text += message + "<br/>"
            pass = False
            Return False
        End If

        Return True
    End Function

    Function regExCheckBottom(ByRef control As String, ByVal expression As String, ByVal message As String) As Boolean

        If Not Regex.IsMatch(control, expression) Then
            lblComplete.Text += message + "<br/>"
            pass = False
            Return False
        End If

        Return True
    End Function


    Protected Sub logout(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogout.ServerClick
        Session.Abandon()
        Response.Redirect("NotLoggedIn.aspx")
    End Sub

    Protected Sub home(ByVal sender As Object, ByVal e As EventArgs) Handles btnhome.ServerClick
        Response.Redirect("AdvisorLandingPage.aspx")
    End Sub

    'Adam Cortez
    'The following functions were not working in the Web Service so I rewrote them with a direction connection to the DB here
    'This checks the DB to ensure the chosen user name doesn't already exist
    Public Function checkUserExists(ByVal UserID As String) As Boolean
        Dim objDB = New DARSDBConnection
        Dim Result As Boolean
        Dim sql = "SELECT UserID From UserTable WHERE UserID='" & UserID & "'"
        Dim ds As DataSet
        ds = objDB.getDataSet(sql)
        If ds.Tables(0).Rows.Count = 0 Then
            Result = False
        Else
            Result = True
        End If
        Return Result
    End Function

    'Adam Cortez
    'creates the user record in the user table and assigns a 1 to the user if it is a student and a 2 if the user is an advisor
    Public Sub createUser(ByVal UserID As String, ByVal PW As String, ByVal Type As Integer)
        Dim objDB = New DARSDBConnection
        Dim sql = "INSERT INTO UserTable (UserID, Password, TypeID) Values " & _
            "('" & UserID & "', '" & PW & "', " & Type & ")"
        objDB.doUpdate(sql)
    End Sub

    'Adam Cortez
    'creates the student record in the the student table with the information from the form
    Public Sub createStudent(ByVal StudentID As String, ByVal LastName As String, ByVal FirstName As String, ByVal Address As String, ByVal City As String, ByVal State As String, ByVal ZipCode As String, ByVal Email As String, ByVal UserID As String, ByVal StudentTypeID As String)
        Dim objDB = New DARSDBConnection
        Dim sql = "INSERT INTO Student (StudentID, LastName, FirstName, StreetAddress, City, State, ZipCode, Email, UserID, StudentTypeID) " & _
            "Values ('" & StudentID & "', '" & LastName & "', '" & FirstName & "', '" & Address & "', '" & City & "', '" & State & "', '" & ZipCode & "', '" & Email & "', '" & UserID & "', " & StudentTypeID & ")"
        objDB.doUpdate(sql)
    End Sub

    'Adam Cortez
    'adds the security questions to the user table for password retrieval
    Public Sub addSecurityQuestions(ByVal UserID As String, ByVal Q1 As String, ByVal A1 As String, ByVal Q2 As String, ByVal A2 As String)
        Dim objDB = New DARSDBConnection
        Dim sql = "UPDATE UserTable SET SecurityQ1 = '" & Q1 & "', SecurityA1 = '" & A1 & "', SecurityQ2 = '" & Q2 & "', SecurityA2 = '" & A2 & "' WHERE UserID = '" & UserID & "'"
        objDB.doUpdate(sql)
    End Sub
End Class